package com.lthlearn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lthlearn.mapper.TravelMapper;
import com.lthlearn.pojo.Result;
import com.lthlearn.pojo.Travel;
import com.lthlearn.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 2942733239@qq.com
 * @date: 2024/11/6 14:50
 * @description: com.lthlearn.service.impl
 * @version: 1.0
 */
@Service
public class TravelServiceImpl implements TravelService {

    @Autowired
    private TravelMapper travelMapper;

    /**
     * 根据 id 查询旅游图片
     *
     * @param id
     * @return
     */
    @Override
    public Travel findById(Integer id) {
        return travelMapper.getById(id);
    }

    /**
     * 根据旅游地点名查询旅游图片
     *
     * @param locationName
     * @return
     */
    @Override
    public Result findByName(String locationName) {
        List<Travel> travels = travelMapper.getByName(locationName);
        if (travels == null || travels.isEmpty()) {
            return new Result("500", "查询失败");
        }
        return new Result("200", "查询成功", travels);
    }

    /**
     * 根据地点名和收藏表中当前用户收藏的所有景点 id 查找收藏表中的景点信息
     *
     * @param userId
     * @param locationName
     * @return
     */
    @Override
    public Result findCollectByName(Integer userId, String locationName) {
        List<Travel> travels = travelMapper.getCollectByName(userId, locationName);
        if (travels == null || travels.isEmpty()) {
            return new Result("500", "查询失败，未收藏");
        }
        return new Result("200", "查询成功", travels);
    }

    /**
     * 分页查询所有数据
     *
     * @param pageNum  当前页
     * @param pageSize 每页显示条数
     * @return
     */
    @Override
    public PageInfo<Travel> findAllByPage(Integer pageNum, Integer pageSize) {
        // 设置分页的参数
        PageHelper.startPage(pageNum, pageSize);
        // 调用 mapper - 查看所有
        List<Travel> travels = travelMapper.getAllByPage();
        // pageInfo 这个对象封装了所有关于分页的信息
        PageInfo<Travel> pageInfo = new PageInfo<>(travels);
        // 返回 pageInfo 对象
        return pageInfo;
    }


}
